<?php
/**
 * Pertyler DatabaseManagement
 * DatabaseModel
 * Database Abstract Class
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

abstract class aDatabase
{
	private $name;

	private $address;

	private $port;

	private $user;

	private $password;

	private static $object;

	public function __Construct($name,
			$address,
			$port,
			$user,
			$password)
	{
		$this->name = $name;

		$this->address = $address;

		$this->port = $port;

		$this->user = $user;

		$this->password = $password;

		if(isset(self::$object))
			throw new Database_exception('There can be only one Database object.');

		self::$object = $this;
	}

	public static function Get()
	{
		if(!isset(self::$object))
		{
			if(!require_once __DIR__ . '/configuration.php')
				throw new Database_exception('Configuration file does not exists.');

			if(empty(self::$object))
				throw new Database_exception('Database object does not exists in the configuration file.');
		}

		return self::$object;
	}

	public function __ToString()
	{
		return $this->name;
	}

	private $postgre;

	protected function postgre()
	{
		require_once __DIR__ . '/../library/Pert/General/Postgre.php';

		if(empty($this->postgre))
			$this->postgre = new \Pert\Postgre($this->__ToString(),
					$this->address,
					$this->port,
					$this->user,
					$this->password);

		return $this->postgre;
	}
}
